/******************************************************************************
 *        Copyright (c) 2005 HOPESEMI Technology Inc.
 *  
 *  All rights reserved. HOPESEMI' source code is an unpublished
 *  work and the use of a copyright notice does not imply otherwise.
 *  This source code contains confidential, trade secret material of
 *  HOPESEMI. Any attempt or participation in deciphering, decoding,
 *  reverse engineering or in any way altering the source code is
 *  strictly prohibited, unless the prior written consent of HOPESEMI
 *  is obtained.ok
 *  
 *  Module:        ustc_api.h
 *
 *  Author:        Philip
 *
 *  DESCRIPTION:
 *                 USTC api header
 *
 *  Modification history
 *  --------------------
 *  17 Apr. 2009: Philip created on DVD-XPS
 *  --------------------
 *
 ******************************************************************************/

#ifndef _USTC_API_H_
#define _USTC_API_H_

/******************************************************************************
 * STRUCTURE:   struct {anonSD_PARA_T}
 * DESCRIPTION: 
 ******************************************************************************/
typedef struct {
	USTC_U32		sd_cmd_timeout;
	USTC_U32		sd_timeout;
	USTC_U16		sd_clk_div;
	USTC_U16		sd_line_mode;
	USTC_U16		ustc_sdc_init_delay1;
	USTC_U16		ustc_sdc_init_delay2;
	USTC_U16		sd_index;
	USTC_U16		resv;
	
	USTC_U32		sd_rca;
	USTC_U32		sd_block_len;
	USTC_U32		sd_block_number;
	USTC_U32		sd_clk_phase_cfg;
}SD_PARA_T;

/******************************************************************************
 * STRUCTURE:   struct _USTC_CAMERA_T_
 * DESCRIPTION: 
 * MEMBERS:
 *  pStart      - 
 *  pData       - 
 *  pDisp       - 
 *  pEnd        - 
 *  wBufSize    - 
 *  wDataCnt    - 
 *  wImageSize  - 
 *  wMark       - 
 *  wFrameCount - 
 *  wBlankFrame - 
 *  wSensorSize - 
 *  wFrameSync  - 
 *  wFitMode    - 
 *  clk_cfg     - 
 *  timing      - 
 *  bEnd        - 
 ******************************************************************************/
typedef struct _USTC_CAMERA_T_
{
	USTC_U16 *pStart;
	USTC_U16 *pData;
	USTC_U16 *pDisp;			//display pointer
	USTC_U16 *pEnd;			//end of frame buffer
	USTC_U32  wBufSize;		//buffer size in half-word
	USTC_U32  wDataCnt;		//valid data count in half-word
	USTC_U32  wImageSize;		//in Half-word
	USTC_U32  wMark;			//data water mark
	USTC_U32  wFrameCount;	//number of frame
	USTC_U32  wBlankFrame;

	USTC_U32  wSensorSize;
	USTC_U32  wFrameSync;
	USTC_U32  wFitMode;
	USTC_U16  clk_cfg;
	USTC_U16  timing;
	USTC_BOOL bEnd;
}USTC_CAMERA_T;

/******************************************************************************
 * STRUCTURE:   struct {anonUSTC_DISK_T}
 * DESCRIPTION: 
 ******************************************************************************/
typedef struct {
	USTC_U32 block_cnt;
	USTC_U16 (*read)(unsigned long *data, unsigned long blk, unsigned long cnt);
	USTC_U16 (*write)(unsigned long *data, unsigned long blk, unsigned long cnt);
	USTC_U16 (*clear)(unsigned long blk, unsigned long cnt);
} USTC_DISK_T;

/******************************************************************************
 * STRUCTURE:   struct _RESIZER_T_
 * DESCRIPTION: 
 * MEMBERS:
 *  x   - 
 *  y   - 
 *  w_w - 
 *  w_h - 
 *  t_x - 
 *  t_y - 
 *  t_w - 
 *  t_h - 
 ******************************************************************************/
typedef struct _RESIZER_T_{
	USTC_U16 x;	//even
	USTC_U16 y;
	USTC_U16 w_w;	//even
	USTC_U16 w_h;
	USTC_U16 t_x;
	USTC_U16 t_y;
	USTC_U16 t_w;	//divided by 4
	USTC_U16 t_h;
}RESIZER_T;

typedef PACKED struct 
{
	/* Sensor Configuration */
	unsigned hsync_shape:1;
	unsigned vsync_shape:1;
	unsigned yuv_format:2;
	unsigned frame_sample_intv:4;
	unsigned sensor_power_ctrl:1;
	unsigned ccir656_enable:2;
	unsigned reserved1:5;
	
	/* CLK Configuration  */
	unsigned clk_fall_edge:1;
	unsigned clk_internal:1;
	unsigned mclk_div:3;
	unsigned pclk_div:3;
	unsigned reserved3:8;

	/* sensor frame start and end flag */
	USTC_U8	frame_finish;
	USTC_U8	line_finish;
	USTC_U8	line_start;
	USTC_U8	frame_start;
}CMR_PARA_T;

#define FRAME_BUFFER_COUNT	2

typedef enum _GS_TP_CMD_E_{
	GS_TP_CMD_SX,
	GS_TP_CMD_SY,
	GS_TP_CMD_DX,
	GS_TP_CMD_DY,
	GS_TP_CMD_PRESS=8,
	GS_TP_CMD_INT=0xC
}GS_TP_CMD_E;

//API for SD Controller
extern STATUS_T ustc_sdc_card_detect(void);
extern STATUS_T ustc_sdc_init(SD_PARA_T * p_para_sd);

extern STATUS_T ustc_sdc_read(unsigned long * p_buffer, unsigned long block_index, unsigned long num);
extern STATUS_T ustc_sdc_write(unsigned long * p_buffer, unsigned long block_index, unsigned long num);

extern void ustc_sdc_read_start(unsigned long blk);
extern void ustc_sdc_write_start(unsigned long blk);
extern void ustc_sdc_read_fifo(unsigned long * buf);
extern void ustc_sdc_write_fifo(unsigned long * buf);
extern void ustc_sdc_set_clk(USTC_U16 clk_div);
extern STATUS_T ustc_sdc_card_state(void);

//API for CMIF Controller
extern USTC_U32	ustc_camera_init_hw(USTC_CAMERA_T * pCamera, USTC_U16 width, USTC_U16 height, USTC_U16 count);
extern void 	ustc_camera_init_sw(USTC_CAMERA_T * pCamera, USTC_U16 * buf, USTC_U32 blankCnt);
extern void 	ustc_camera_scale(USTC_CAMERA_T * pCamera, RESIZER_T * p_resizer);
extern void		ustc_camera_reset(USTC_CAMERA_T * pCamera);
extern void		ustc_camera_open(USTC_CAMERA_T * pCamera);

//API for USB Controller
extern void ustc_udisk_init(USTC_U32 *cache, USTC_U8 *vendor, USTC_U8 *product);
extern void ustc_udisk_isr(void);
extern void ustc_udisk_process(void);
extern void ustc_udisk_mount(USTC_DISK_T *pDisk);
extern void ustc_udisk_start(void);
extern void ustc_udisk_stop(void);


//IO Control
extern GS_CHAR* ustc_get_version(void);
extern USTC_U16 ustc_int_status(void);
extern void ustc_int_enable(USTC_U16 module);
extern void ustc_int_disable(USTC_U16 module);
extern void ustc_int_clear(USTC_U16 status);
extern void ustc_sw_reset(USTC_U16 module, USTC_U32 during);
extern void ustc_sw_reset_enter(USTC_U16 module);
extern void ustc_sw_reset_exit(USTC_U16 module);
extern void ustc_cfg_pll(USTC_U16 pll);
extern void ustc_cfg_cmr_snr_clk(USTC_U16 pclk_edge, USTC_U16 mclk_div, USTC_U16 pclk_div);
extern void ustc_cfg_cmr_tv_clk(USTC_U16 pclk_edge, USTC_U16 mclk_div, USTC_U16 pclk_div);
extern void ustc_mclk_enable(void);
extern void ustc_mclk_disable(void);
extern void ustc_cfg_yrc_para(USTC_U16 low, USTC_U16 hig);
extern void ustc_sys_ctrl(USTC_U16 para);
extern void ustc_clk_enable(USTC_U16 module);
extern void ustc_clk_disable(USTC_U16 module);
extern USTC_U16 ustc_cfifo_data_count(void);
extern void ustc_cfifo_set(void);
extern void ustc_cfifo_reset(void);
extern USTC_U16 ustc_get_id(void);

//TP
extern void ustc_tp_init(void);
extern void ustc_tp_cmd(GS_TP_CMD_E cmd);
extern USTC_U16 ustc_tp_value(void);
extern void ustc_tp_position(USTC_U16 * x, USTC_U16 * y, USTC_U16 * z);
extern void ustc_tp_cfg_clk(USTC_U16 clk_div);
extern void ustc_tp_cfg_mode(USTC_BOOL turbo_mode);
extern void ustc_tp_int_enable(void);
extern void ustc_tp_int_disable(void);
extern void ustc_tp_power(USTC_BOOL power_down);
extern void ustc_tp_sleep(USTC_BOOL chip_sleep);
extern void ustc_tp_sample_mode(USTC_BOOL double_sample);
extern void ustc_tp_switch(USTC_U16 val);

#endif
